﻿//https://leetcode.cn/problems/longest-common-prefix/description/
class Solution
{
	public :
	string longestCommonPrefix(vector<string>& strs)
	{
		// 解法⼀：两两⽐较
		string ret = strs[0];
		for (int i = 1; i < strs.size(); i++)
			ret = findCommon(ret, strs[i]);
		return ret;
	} 
	string findCommon(string& s1, string& s2)
	{
		int i = 0;
		while (i < min(s1.size(), s2.size()) && s1[i] == s2[i]) i++;
		return s1.substr(0, i);
	}
};

class Solution
{
	public :
		string longestCommonPrefix(vector<string>& strs)
	{
		// 解法⼆：统⼀⽐较
		for (int i = 0; i < strs[0].size(); i++)
		{
			char tmp = strs[0][i];
			for (int j = 1; j < strs.size(); j++)
				if (i == strs[j].size() || tmp != strs[j][i])
					return strs[0].substr(0, i);
		} return strs[0];
	}
};